// UART_commundate_indexcatdate_indexon.date_indexno
int LED = 13;
byte  Buffer[3] = {0x00, 0x00, 0x00};
void Communication_Decode(void)
{
  if (Buffer[0] == 0x00)
  {
    switch (Buffer[1])
    {
      case 0x01: digitalWrite(LED, 1); return;
      case 0x02: digitalWrite(LED, 0); return;
      case 0x03: digitalWrite(LED, 1); return;
      case 0x04: return;
      case 0x00: return;
      case 0x05: return;
      case 0x06: return;
      case 0x07: return;
      case 0x08: return;
      default: return;
    }
  }
  else if (Buffer[0] == 0x01)
  {
    if (Buffer[2] > 180)
      return;
    switch (Buffer[1])
    {
      case 0x01: return;
      case 0x02: return;
      case 0x03: return;
      case 0x04: return;
      case 0x05: return;
      default : return;
    }
  }
  else if (Buffer[0] == 0x02)
  {
    if (Buffer[2] > 10)
      return;
    switch (Buffer[1])
    {
      case 0x01: return;
      case 0x02: return;
      default : return;
    }
  }
  else
  {
    return;
  }
}
//  0    1    2    3    4    5    6    7
byte Transmit_byte[8] = {0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f};
void Transmit_variable(byte Type, float Value)
{
  byte int0, int1, int2, int3, int4;
  Value = Value * 100;
  int1 = int(Value) / 1000;
  int2 = int(Value) % 1000 / 100;
  int3 = int(Value) % 1000 % 100 / 10;
  int4 = int(Value) % 1000 % 100 % 10 / 1;
  Transmit_byte[2] = Type;
  Transmit_byte[3] = int1;
  Transmit_byte[4] = int2;
  Transmit_byte[5] = int3;
  Transmit_byte[6] = int4;
  for (int i = 0; i < 8; i++)
  {
    Serial.print(Transmit_byte[i], HEX);
  }
  delay(2);
}
void setup() {
  Serial.begin(115200);
  Serial.flush();
  pinMode(LED, OUTPUT);
}

byte SBUF = 0x00;
int rec_flag = 0; //等于0等待接受 等于1正在接受
int index = 0;

void loop()
{
  if (Serial.available() > 0) //如果Arduino控制板的读缓冲区中存在上位机下达的字节
  {
    SBUF = Serial.read();
    //delay(2);
    //Serial.print(SBUF,HEX);
    if (SBUF != -1)
    {
      if (rec_flag == 0)
      {
        if (SBUF == 0xff)
        {
          rec_flag = 1;
          index = 0;
        }
      }
      else
      {
        if (SBUF == 0xff)
        {
          rec_flag = 0;
          if (index == 3)
          {
            Communication_Decode();
          }
          index = 0;
        }
        else
        {
          Buffer[index] = SBUF;
          //Serial.print(Buffer[index],HEX);
          index++;
        }
      }
    }
  }
  for (int j = 0; j < 10; j++)
  {
    Transmit_variable(0x01, 10 * sin(j * 2 * 3.14 / 10.0) + 80);
    Transmit_variable(0x02, 10 * cos(j * 2 * 3.14 / 10.0) + 40);
    Transmit_variable(0x03, 5 * cos(j * 2 * 3.14 / 10.0) + 20);
    //delay(2);
  }
}
